﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class NetworkList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            networkList.OnClientClick = "onSelectRow();";
            if (!IsPostBack)
            {
                Networks nets = Networks.GetAllActiveNetworks();
                networkList.NetworkList = nets;
                networkList.Refresh();
                btnSave.Enabled = false;
                btnCancel.Enabled = false;
            }

            btnAdd.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            btnEdit.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            btnDelete.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
        }

        public void btnLoad_OnClick(Object sender, EventArgs e)
        {
            try
            {
                if (hidSelectedID.Value == "")
                    return;
                int id = int.Parse(hidSelectedID.Value);
                string oldFilter = networkList.NetworkList.Filter;
                networkList.NetworkList.Filter = Networks.ColumnNames.ID + "=" + id.ToString();

                editNet.NetworkName = networkList.NetworkList.NetworkName;
                editNet.DealerCode = networkList.NetworkList.DealerCode;
                editNet.DealerManagerEmail = networkList.NetworkList.DealerManagerEmail;
                editNet.DealerManagerName = networkList.NetworkList.DealerManagerName;
                editNet.DealerManagerPhone = networkList.NetworkList.DealerManagerPhone;
                editNet.CommissionEnquiryEmail = networkList.NetworkList.CommissionEnquiryEmail;
                editNet.CommissionEnquiryName = networkList.NetworkList.CommissionEnquiryName;
                editNet.CommissionEnquiryPhone = networkList.NetworkList.CommissionEnquiryPhone;
                editNet.OtherEmail = networkList.NetworkList.OtherEmail;
                editNet.OtherName = networkList.NetworkList.OtherName;
                editNet.OtherPhone = networkList.NetworkList.OtherPhone;
                editNet.PostalAddress = networkList.NetworkList.PostalAddress;

                networkList.NetworkList.Filter = oldFilter;

                ((Popup)Master).ShowMessage("Ready");
                ((Popup)Master).UpdateStatus();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("NetworkList.aspx.cs", "Load", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ((Popup)Master).UpdateStatus();
            }
        }

        void Reset()
        {
            editNet.NetworkName = "";
            editNet.DealerCode = "";
            editNet.DealerManagerEmail = "";
            editNet.DealerManagerName = "";
            editNet.DealerManagerPhone = "";
            editNet.CommissionEnquiryEmail = "";
            editNet.CommissionEnquiryName = "";
            editNet.CommissionEnquiryPhone = "";
            editNet.OtherEmail = "";
            editNet.OtherName = "";
            editNet.OtherPhone = "";
            editNet.PostalAddress = "";
        }

        void Save()
        {
            try
            {
                networkList.NetworkList.NetworkName = editNet.NetworkName;
                networkList.NetworkList.DealerCode = editNet.DealerCode;
                networkList.NetworkList.DealerManagerEmail = editNet.DealerManagerEmail;
                networkList.NetworkList.DealerManagerName = editNet.DealerManagerName;
                networkList.NetworkList.DealerManagerPhone = editNet.DealerManagerPhone;
                networkList.NetworkList.CommissionEnquiryEmail = editNet.CommissionEnquiryEmail;
                networkList.NetworkList.CommissionEnquiryName = editNet.CommissionEnquiryName;
                networkList.NetworkList.CommissionEnquiryPhone = editNet.CommissionEnquiryPhone;
                networkList.NetworkList.OtherEmail = editNet.OtherEmail;
                networkList.NetworkList.OtherName = editNet.OtherName;
                networkList.NetworkList.OtherPhone = editNet.OtherPhone;
                networkList.NetworkList.PostalAddress = editNet.PostalAddress;
                networkList.NetworkList.LastModifiedByUser = SessionUser.ID;
                networkList.NetworkList.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                networkList.NetworkList.Save();


                ((Popup)Master).ShowMessage("Saved");
                ((Popup)Master).UpdateStatus();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("NetworkList.aspx.cs", "Save", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ((Popup)Master).UpdateStatus();
            }
        }

        public void btnAdd_OnClick(Object sender, EventArgs e)
        {
            hidSelectedID.Value = "";
            editNet.Enable = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            Reset();
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            string oldFilter = "";
            if (hidSelectedID.Value == "")
            {
                networkList.NetworkList.AddNew();
                networkList.NetworkList.Deleted = false;
            }
            else
            {
                int id = int.Parse(hidSelectedID.Value);
                oldFilter = networkList.NetworkList.Filter;
                networkList.NetworkList.Filter = Networks.ColumnNames.ID + "=" + id.ToString();
            }

            Save();

            if(oldFilter != "")
                networkList.NetworkList.Filter = oldFilter;

            networkList.Refresh();
            
            ((Popup)Master).UpdateAll();
        }

        public void btnCancel_OnClick(Object sender, EventArgs e)
        {
            editNet.Enable = false;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
        }

        public void btnEdit_OnClick(Object sender, EventArgs e)
        {
            editNet.Enable = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }

        public void btnDelete_OnClick(Object sender, EventArgs e)
        {
            string oldFilter = "";
            if (hidSelectedID.Value != "")
            {
                int id = int.Parse(hidSelectedID.Value);
                oldFilter = networkList.NetworkList.Filter;
                networkList.NetworkList.Filter = Networks.ColumnNames.ID + "=" + id.ToString();
            }
            else
                return;

            networkList.NetworkList.Deleted = true;
            networkList.NetworkList.LastModifiedByUser = SessionUser.ID;
            networkList.NetworkList.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            networkList.NetworkList.Save();

            if (oldFilter != "")
                networkList.NetworkList.Filter = oldFilter;

            RefreshNetworkList();

            ((Popup)Master).UpdateAll();
        }

        void RefreshNetworkList()
        {
            networkList.CurrentPageIndex = 0;
            networkList.Filter = "Deleted = 0";
            networkList.Refresh();
        }
    }
}
